home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Storage / ODStor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  14.8 KB  |  546 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ODStor.cpp
  3.  
  4.     Contains:    Implementation of ODStorageSystem
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     3/15/96    DM        1295410: create list iterators on stack
  13.                                     (avoid mem thrash during purge)
  14.         <28>    10/24/95    jpa        1293441: VL: Better error handling closing
  15.                                     containers.
  16.         <27>     10/8/95    TJ        Fixes Recomended by Refball
  17.         <26>      9/8/95    VL        1282012: Replaced kODErrContainerNotExists
  18.                                     with kODErrContainerDoesNotExist.
  19.         <25>      9/6/95    VL        1279074: Replaced THROW_IF_NULL with
  20.                                     ODSetSOMException.
  21.         <24>     8/30/95    EL        1279832: Storage THROW_IF_NULL problems.
  22.         <23>     8/26/95    TÇ        1274606 FB2: Patching Remarks
  23.         <22>     8/16/95    NP        1274946: ErrorDef.idl problems. Add include
  24.                                     file.
  25.         <21>     8/12/95    TÇ        1276806 Optimization: use kODFalse instead
  26.                                     of kODTrue in comparisons
  27.         <20>      8/3/95    RR        #1257260: Collapse B classes. Remove
  28.                                     somInit methods. Don't call IsInitialized
  29.                                     or SubclassResponsibility
  30.         <19>     7/21/95    VL        1270320: Dispose ba._buffer from
  31.                                     container->GetID.
  32.         <18>     6/14/95    EL        #1258050: Throw if container suite is
  33.                                     missing.
  34.         <17>      6/1/95    jpa        Handle errs thrown from ODNewObject.
  35.                                     [1242839]
  36.         <16>     5/26/95    VL        1251403: Multithreading naming support.
  37.         <15>     4/15/95    VL        1240014: Used new ContainerList::Add which
  38.                                     does not take a ContainerID.
  39.         <14>     3/23/95    VL        1230357: implemented NeedSpace.
  40.         <13>      2/2/95    CG        #1195019:  ValueNameSpace entries are now
  41.                                     copied in and out instead of pointers being
  42.                                     passed around.
  43.         <12>     1/23/95    CG        #1195019: ValueNameSpace methods now take
  44.                                     ODByteArray parameter instead of ODPtr.
  45.         <11>    11/17/94    CG        #1181489: Turned off STATICBENTO, Bento now
  46.                                     dynamically linked.
  47.         <10>    11/14/94    VL        1188257: Stopped using Bento errors.
  48.          <9>     9/23/94    VL        1184272: ContainerID is now a sequence of
  49.                                     octets.
  50.          <8>      9/9/94    jpa        Added sufficient SOM_CATCHes to let OpenDoc
  51.                                     run normally. Many more are needed; I'll
  52.                                     leave that to Vincent. [1185656]
  53.          <7>      9/5/94    VL        1184871: Used Renew to remove dependency on
  54.                                     default heap.
  55.          <6>     8/19/94    TÇ        #1181622 Fix Written to Owned & name &
  56.                                     reference to ASLM Build
  57.          <5>     8/17/94    CG        #1181489: Made ready for Container Suite
  58.                                     instantiation by name.  ASLM BUILD ->
  59.                                     STATICBENTO.
  60.          <4>     8/11/94    VL        1180299: Added CreateTypeList and
  61.                                     CreatePlatformTypeList.
  62.          <3>      8/5/94    VL        Changed ODBaseSession to ODSession.
  63.          <2>      8/5/94    VL        1171603: Removed Get/SetCMSession.
  64.          <1>      8/3/94    VL        first checked in
  65.          <4>     7/29/94    CG        NameSpace API changes.
  66.          <3>      7/7/94    VL        Commented out ODRecoverHeapID.
  67.          <2>     6/28/94    VL        Use ODGetMemoryHeapID.
  68.          <1>     6/28/94    VL        first checked in
  69.  
  70.     To Do:
  71.         **** ERROR HANDLING!!!! ****
  72.     In Progress:
  73.         
  74. */
  75.  
  76. #define ODStorageSystem_Class_Source
  77. #define VARIABLE_MACROS
  78. #include <ODStor.xih>
  79.  
  80. #ifndef _EXCEPT_
  81. #include "Except.h"
  82. #endif
  83.  
  84. #ifndef _STORPRIV_
  85. #include "StorPriv.h"
  86. #endif
  87.  
  88. #ifndef SOM_ODPart_xh
  89. #include <Part.xh>
  90. #endif
  91.  
  92. #ifndef SOM_ODNameSpaceManager_xh
  93. #include <NmSpcMg.xh>
  94. #endif
  95.  
  96. #ifndef SOM_ODSession_xh
  97. #include <ODSessn.xh>
  98. #endif
  99.  
  100. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  101. #include <StdDefs.xh>
  102. #endif
  103.  
  104. #ifndef SOM_ODTranslation_xh
  105. #include <Translt.xh>
  106. #endif
  107.  
  108. #ifndef _ISOSTR_
  109. #include "ISOStr.h"
  110. #endif
  111.  
  112. #ifndef SOM_ODBinding_xh
  113. #include <ODBindng.xh>
  114. #endif
  115.  
  116. #ifndef SOM_ODTypeList_xh
  117. #include <TypeList.xh>
  118. #endif
  119.  
  120. #ifndef SOM_ODPlatformTypeList_xh
  121. #include <PfTypLs.xh>
  122. #endif
  123.  
  124. #ifndef _ODNEW_
  125. #include <ODNew.h>
  126. #endif
  127.  
  128. #ifndef _ODMemory_
  129. #include <ODMemory.h>
  130. #endif
  131.  
  132. #ifndef _UTILERRS_
  133. #include "UtilErrs.h"
  134. #endif
  135.  
  136. //#define STATICBENTO
  137. #ifndef STATICBENTO
  138.  
  139.     #ifndef _ODNEWOBJ_
  140.     #include "ODNewObj.h"
  141.     #endif
  142.     
  143.     #ifndef __STRING__
  144.     #include <string.h>        // Only for comparing ContainerType. Should be removed when we use DLL.
  145.     #endif
  146.  
  147. #else
  148.  
  149.     #ifndef SOM_ODFileContainer_xh
  150.     #include <FileCtr.xh>
  151.     #endif
  152.  
  153.     #ifndef SOM_ODEmbeddedContainer_xh
  154.     #include <EmbedCtr.xh>
  155.     #endif
  156.     
  157.     #ifndef SOM_ODMemContainer_xh
  158.     #include <MemCtr.xh>
  159.     #endif
  160.     
  161. #endif
  162.  
  163. //==============================================================================
  164. // Constants
  165. //==============================================================================
  166.  
  167.  
  168. //==============================================================================
  169. // Function Prototype
  170. //==============================================================================
  171.  
  172. #ifdef STATICBENTO
  173.  
  174. static ODFileContainer* NewODFileContainer(ODMemoryHeapID heapID);
  175. static ODMemContainer* NewODMemContainer(ODMemoryHeapID heapID);
  176. static ODEmbeddedContainer* NewODEmbeddedContainer(ODMemoryHeapID heapID);
  177.  
  178. #endif
  179.  
  180. #pragma segment ODStorageSystem
  181.  
  182. #include "ODStorB.cpp"    // Platform-independent methods, if any
  183.  
  184. SOM_Scope ODMemoryHeapID  SOMLINK ODStorageSystemGetHeap(ODStorageSystem *somSelf, Environment *ev)
  185. {
  186.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  187.     ODStorageSystemMethodDebug("ODStorageSystem","GetHeap");
  188.  
  189.     return _fHeap;
  190. }
  191.  
  192. SOM_Scope void  SOMLINK ODStorageSystemsomUninit(ODStorageSystem *somSelf)
  193. {
  194.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  195.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemsomUninit");
  196.  
  197.     if (_fContainers)
  198.     {
  199.         TRY{
  200.             Environment* ev = somGetGlobalEnvironment();
  201.             
  202.             ContainerListIterator iter(_fContainers);
  203.             iter.Initialize();
  204.         
  205.             ODContainer*    container = iter.Last();
  206.             while (container != kODNULL) {
  207.                 container->Close(ev);
  208.                 delete container;
  209.                 container = iter.Previous();
  210.             }
  211.             // delete iter;
  212.             
  213.             delete _fContainers;
  214.         }CATCH_ALL{
  215.         }ENDTRY
  216.     }
  217. }
  218.  
  219. SOM_Scope ODSize  SOMLINK ODStorageSystemPurge(ODStorageSystem *somSelf, Environment *ev,
  220.         ODSize size)
  221. {
  222.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  223.     ODStorageSystemMethodDebug("ODStorageSystem","Purge");
  224.  
  225.     ODULong sizePurged = 0; ODVolatile( sizePurged );
  226.     
  227.     SOM_TRY
  228.     
  229.         ContainerListIterator iter(_fContainers);
  230.         iter.Initialize();
  231.     
  232.         ODContainer*    container = iter.Last();
  233.         while (container != kODNULL) {
  234.             TRY
  235.                 sizePurged += container->Purge(ev, size-sizePurged);
  236.             CATCH_ALL
  237.             ENDTRY
  238.             // dh - I did't wrap the iterator call in a TRY block, because there is no
  239.             // use continuing with the iterator if there is a problem with it.
  240.             container = iter.Previous();
  241.         }
  242.         // delete iter;
  243.  
  244.         sizePurged += parent_Purge(somSelf,ev,size);
  245.     SOM_CATCH_ALL
  246.         WARN("Error %ld trying to purge in ODStorageSystemPurge",ErrorCode());
  247.         SetErrorCode(kODNoError);        // dh - Eat the exception; Purge should not 
  248.                                         // propagate it because clients function
  249.                                         // fine whether memory was purged or not.
  250.     SOM_ENDTRY
  251.     
  252.     return sizePurged;
  253. }
  254.  
  255. SOM_Scope void  SOMLINK ODStorageSystemInitStorageSystem(ODStorageSystem *somSelf, Environment *ev,
  256.         ODSession* session)
  257. {
  258.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  259.     ODStorageSystemMethodDebug("ODStorageSystem","InitStorageSystem");
  260.  
  261.     SOM_TRY
  262.     
  263.     /* Moved from somInit. SOM itself sets fields to zero
  264.     _fSession = kODNULL;
  265.     _fHeap = kDefaultHeapID;
  266.     _fContainers = kODNULL;
  267.     */
  268.     _fSession = session;
  269.     _fHeap = ODRecoverHeapID(somSelf);
  270.     
  271.     _fContainers = new(somSelf->GetHeap(ev)) ContainerList;
  272.     _fContainers->Initialize();
  273.     
  274.     SOM_CATCH_ALL
  275.     SOM_ENDTRY
  276. }
  277.  
  278. SOM_Scope ODSession*  SOMLINK ODStorageSystemGetSession(ODStorageSystem *somSelf, Environment *ev)
  279. {
  280.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  281.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemGetSession");
  282.  
  283.     return _fSession;
  284. }
  285.  
  286. SOM_Scope ODContainer*  SOMLINK ODStorageSystemAcquireContainer(ODStorageSystem *somSelf, Environment *ev,
  287.         ODContainerType containerType,
  288.         ODContainerID* id)
  289. {
  290.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  291.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemAcquireContainer");
  292.     
  293.     SOM_CATCH return kODNULL;
  294.  
  295.     ODContainer*    container = kODNULL;
  296.     
  297.     if ((container = _fContainers->Get(id)) != kODNULL) {
  298.         container->Acquire(ev);
  299.     }
  300.     else {
  301. #ifndef STATICBENTO
  302.  
  303.         ODContainerSuite theContainerSuite = 
  304.             _fSession->GetBinding(ev)->GetContainerSuite(ev,containerType);
  305.         
  306.         if ( theContainerSuite )
  307.         {
  308.             TRY
  309.                 container = (ODContainer*) ODNewObject(theContainerSuite);
  310.             CATCH_ALL
  311.                 ODDisposePtr( theContainerSuite );
  312.                 THROW( ErrorCode()==kODErrCantLoadSOMClass ?kODErrCannotCreateContainer
  313.                                                            :ErrorCode() );
  314.             ENDTRY
  315.             ODDisposePtr( theContainerSuite );
  316.         }
  317.         else
  318.             THROW( kODErrCannotCreateContainer );
  319.             
  320. #else
  321.         // Hardwire for ODContainer for now. We should do the dynamic loading
  322.         //    of the class using containerType in the future.
  323.  
  324.         if (strcmp(kODDefaultFileContainer, containerType) == 0)
  325.             container = NewODFileContainer(somSelf->GetHeap(ev));
  326.         else if (strcmp(kODBentoEmbeddedContainer, containerType) == 0)
  327.             container = NewODEmbeddedContainer(somSelf->GetHeap(ev));
  328.         else if (strcmp(kODDefaultMemoryContainer, containerType) == 0)
  329.             container = NewODMemContainer(somSelf->GetHeap(ev));
  330.             
  331. #endif            
  332.         container->InitContainer(ev, somSelf, id);
  333.         
  334.         _fContainers->Add(container);
  335.         
  336.         container->Open(ev);
  337.     }
  338.     return container;
  339. }
  340.  
  341. SOM_Scope ODContainer*  SOMLINK ODStorageSystemCreateContainer(ODStorageSystem *somSelf, Environment *ev,
  342.         ODContainerType containerType,
  343.         ODContainerID* id)
  344. {
  345.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  346.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemCreateContainer");
  347.  
  348.     SOM_CATCH return kODNULL;
  349.  
  350.     ODContainer*    container = kODNULL;
  351.     
  352.     if ((container = _fContainers->Get(id)) != kODNULL)
  353.         THROW(kODErrContainerExists);
  354.     
  355. #ifndef STATICBENTO
  356.  
  357.     ODContainerSuite theContainerSuite = 
  358.         _fSession->GetBinding(ev)->GetContainerSuite(ev,containerType);
  359.     
  360.     if ( theContainerSuite )
  361.     {
  362.         TRY
  363.             container = (ODContainer*) ODNewObject(theContainerSuite);
  364.         CATCH_ALL
  365.             ODDisposePtr( theContainerSuite );
  366.             THROW( ErrorCode()==kODErrCantLoadSOMClass ?kODErrCannotCreateContainer
  367.                                                        :ErrorCode() );
  368.         ENDTRY
  369.         ODDisposePtr( theContainerSuite );
  370.     }
  371.     else
  372.         THROW( kODErrCannotCreateContainer );
  373.  
  374. #else
  375.     // Hardwire for the two Bento Containers for now. We should do the dynamic loading
  376.     //    of the class using containerType in the future.
  377.  
  378.     if (strcmp(kODDefaultFileContainer, containerType) == 0)
  379.         container = NewODFileContainer(somSelf->GetHeap(ev));
  380.     else if (strcmp(kODBentoEmbeddedContainer, containerType) == 0)
  381.             container = NewODEmbeddedContainer(somSelf->GetHeap(ev));
  382.     else if (strcmp(kODDefaultMemoryContainer, containerType) == 0)
  383.             container = NewODMemContainer(somSelf->GetHeap(ev));
  384.             
  385. #endif
  386.  
  387.     container->InitContainer(ev, somSelf, id);
  388.     
  389.     _fContainers->Add(container);
  390.     
  391.     container->Create(ev);
  392.     container->Open(ev);
  393.  
  394.     return container;
  395. }
  396.  
  397. SOM_Scope void  SOMLINK ODStorageSystemNeedSpace(ODStorageSystem *somSelf, Environment *ev,
  398.         ODSize memSize,
  399.         ODBoolean doPurge)
  400. {
  401.     //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  402.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemNeedSpace");
  403.  
  404.     if (doPurge != kODFalse) {
  405.         SOM_TRY
  406.         somSelf->Purge(ev, memSize);
  407.         SOM_CATCH_ALL
  408.         SOM_ENDTRY
  409.     }
  410. }
  411.  
  412. SOM_Scope ODTypeList*  SOMLINK ODStorageSystemCreateTypeList(ODStorageSystem *somSelf, Environment *ev,
  413.         ODTypeList* typeList)
  414. {
  415.     //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  416.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemCreateTypeList");
  417.  
  418.     ODTypeList* tmp; ODVolatile(tmp);
  419.     
  420.     SOM_TRY
  421.     
  422.     tmp = new ODTypeList;
  423.     THROW_IF_NULL(tmp);
  424.     tmp->InitTypeList(ev, typeList);
  425.     
  426.     SOM_CATCH_ALL
  427.         ODDeleteObject(tmp);
  428.     SOM_ENDTRY
  429.     return tmp;
  430. }
  431.  
  432. SOM_Scope ODPlatformTypeList*  SOMLINK ODStorageSystemCreatePlatformTypeList(ODStorageSystem *somSelf, Environment *ev,
  433.         ODPlatformTypeList* typeList)
  434. {
  435.     //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  436.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemNeedSpace");
  437.     
  438.     ODPlatformTypeList* tmp;
  439.     
  440.     SOM_TRY
  441.  
  442.     tmp = new ODPlatformTypeList;
  443.     if (tmp == kODNULL)
  444.         ODSetSOMException(ev, kODErrOutOfMemory);
  445.     else
  446.         tmp->InitPlatformTypeList(ev, typeList);
  447.         
  448.     SOM_CATCH_ALL
  449.         tmp = kODNULL;
  450.     SOM_ENDTRY
  451.  
  452.     
  453.     return tmp;
  454. }
  455.  
  456.  
  457. SOM_Scope void  SOMLINK ODStorageSystemGrowHeap(ODStorageSystem *somSelf, Environment *ev,
  458.         ODSize memSize)
  459. {
  460.     //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  461.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemGrowHeap");
  462.  
  463. ODUnused(memSize);
  464. }
  465.  
  466. SOM_Scope ODStorageSystem*  SOMLINK ODStorageSystemReleaseContainer(ODStorageSystem *somSelf, Environment *ev,
  467.         ODContainer* container)
  468. {
  469.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  470.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemReleaseContainer");
  471.  
  472.     SOM_CATCH return somSelf;
  473.  
  474.     ODContainerID containerID = container->GetID(ev);
  475.         
  476.     if ((container = _fContainers->Get(&containerID)) != kODNULL) {
  477.         _fContainers->Remove(&containerID);
  478.         ODDisposePtr(containerID._buffer);
  479.         TRY
  480.             container->Close(ev);
  481.             delete container;
  482.         CATCH_ALL
  483.             SetErrorCode(kODNoError);
  484.         ENDTRY
  485.     }
  486.     else
  487.     {
  488.         ODDisposePtr(containerID._buffer);
  489.         THROW(kODErrContainerDoesNotExist);
  490.     }
  491.         
  492.     return somSelf;
  493. }
  494.  
  495. #ifdef STATICBENTO
  496.  
  497. //------------------------------------------------------------------------------
  498. // NewODFileContainer
  499. //------------------------------------------------------------------------------
  500.  
  501. static ODFileContainer* NewODFileContainer(ODMemoryHeapID heapID)
  502. {
  503.     SOMClass*    fileCtrClass = ODFileContainerNewClass(ODFileContainer_MajorVersion,
  504.                                                         ODFileContainer_MinorVersion);
  505.     ODULong        size = fileCtrClass->somGetInstanceSize();
  506.     ODPtr        buffer = ODNewPtr(size, heapID);
  507.     ODFileContainer*    fileCtr = (ODFileContainer*) fileCtrClass->somRenew(buffer);
  508.     
  509.     return fileCtr;
  510. }
  511.  
  512. //------------------------------------------------------------------------------
  513. // NewODMemContainer
  514. //------------------------------------------------------------------------------
  515.  
  516. static ODMemContainer* NewODMemContainer(ODMemoryHeapID heapID)
  517. {
  518.     SOMClass*    memCtrClass = ODMemContainerNewClass(ODMemContainer_MajorVersion,
  519.                                                         ODMemContainer_MinorVersion);
  520.     ODULong        size = memCtrClass->somGetInstanceSize();
  521.     ODPtr        buffer = ODNewPtr(size, heapID);
  522.     ODMemContainer*    memCtr = (ODMemContainer*) memCtrClass->somRenew(buffer);
  523.     
  524.     return memCtr;
  525. }
  526.  
  527. //------------------------------------------------------------------------------
  528. // NewODEmbeddedContainer
  529. //------------------------------------------------------------------------------
  530.  
  531. static ODEmbeddedContainer* NewODEmbeddedContainer(ODMemoryHeapID heapID)
  532. {
  533.     SOMClass*    embeddedCtrClass = ODEmbeddedContainerNewClass(ODEmbeddedContainer_MajorVersion,
  534.                                                                 ODEmbeddedContainer_MinorVersion);
  535.     ODULong        size = embeddedCtrClass->somGetInstanceSize();
  536.     ODPtr        buffer = ODNewPtr(size, heapID);
  537.     ODEmbeddedContainer*    embeddedCtr = (ODEmbeddedContainer*) embeddedCtrClass->somRenew(buffer);
  538.     
  539.     return embeddedCtr;
  540. }
  541.  
  542. #endif
  543.  
  544.  
  545.  
  546.